package br.com.locadoracom.dao;

import java.util.List;

import br.com.locadoracom.model.Dependente;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class DependenteDao extends Dao<Dependente>{
	EntityManagerFactory  emf = Persistence.createEntityManagerFactory("Locadora");
	EntityManager em = emf.createEntityManager();
	
	@Override
	public void create(Dependente dependente) {
		em.getTransaction().begin();
		em.persist(dependente);
		em.getTransaction().commit();
	}

	@Override
	public Dependente merge(Dependente dependente) {
		Dependente temp;
		em.getTransaction().begin();
		temp = em.merge(dependente);
		em.getTransaction().commit();
		return temp;
	}

	@Override
	public void remove(Dependente dependente) {
		em.getTransaction().begin();
		em.remove(dependente);
		em.getTransaction().commit();
	}
	
	public Dependente findById(Integer Id){
		return em.find(Dependente.class, Id);
	}
	
	@SuppressWarnings("unchecked")
	public List<Dependente> findByAll() throws Exception{
		String strQuery="select d from Dependente d";
        Query rsQuery = em.createQuery(strQuery);
		return  rsQuery.getResultList();
	}
	
}